/*
 * Created by JFormDesigner on Sat Aug 16 17:36:30 CST 2025
 */

package org.xl.main.jenkins;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.nio.charset.StandardCharsets;
import javax.swing.*;

/**
 * @author Windows
 */
public class UserPwdChangeFrame extends JFrame {
    public UserPwdChangeFrame() {
        initComponents();
    }

    private void mdBtnOk(ActionEvent e) {
        // TODO add your code here
        String source = new String(passwordField1.getPassword());
        String target = new String(passwordField2.getPassword());
        if (!source.equals(target)) {
            JOptionPane.showMessageDialog(this, "Passwords don't match!", "Warning", JOptionPane.WARNING_MESSAGE);
        } else {

            try {
                String path = this.getClass().getClassLoader().getResource("config/jenkins_user.properties").getPath();
                if (!File.separator.equals(":")) {
                    path = path.substring(1);
                }
                String s = FileUtil.readString(path, StandardCharsets.UTF_8);
                s = s.replaceAll("password=.*", "password=" + source);
                FileUtil.writeString(s, new File(path), StandardCharsets.UTF_8);
                JOptionPane.showMessageDialog(this, "修改成功");
                this.dispose();
            } catch (IORuntimeException ex) {
                JOptionPane.showMessageDialog(this, "修改失败" + ex.getMessage());
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        passwordField1 = new JPasswordField();
        passwordField2 = new JPasswordField();
        label2 = new JLabel();
        button1 = new JButton();

        //======== this ========
        setTitle("\u4fee\u6539\u5bc6\u7801");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8f93\u5165\u5bc6\u7801\uff1a");
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(70, 45), label1.getPreferredSize()));
        contentPane.add(passwordField1);
        passwordField1.setBounds(135, 40, 170, passwordField1.getPreferredSize().height);
        contentPane.add(passwordField2);
        passwordField2.setBounds(135, 90, 170, 25);

        //---- label2 ----
        label2.setText("\u786e\u8ba4\u5bc6\u7801:");
        contentPane.add(label2);
        label2.setBounds(70, 95, 60, 17);

        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.addActionListener(e -> mdBtnOk(e));
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(170, 130), button1.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JPasswordField passwordField1;
    private JPasswordField passwordField2;
    private JLabel label2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
